﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.ComponentModel;

namespace Pavilion.DesignerTool
{
    public class DrawingBoardViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public UndoRedoController UndoRedoController { get { return CurrentLayer == null ? null : CurrentLayer.UndoRedoController; } }
        public double Width { get; set; }
        public double Height { get; set; }

        private DrawingBoardXnaComponent xnaComponent;
        private TextureData CurrentLayer { get { return xnaComponent.DrawingBoardData.TextureData; } }
        private Microsoft.Xna.Framework.Graphics.Texture2D previousTexture;

        public DrawingBoardViewModel(DrawingBoardXnaComponent xnaComponent)
        {
            this.xnaComponent = xnaComponent;
        }

        public void LoadDrawTool(IDrawTool drawTool)
        {
            xnaComponent.DrawTool = drawTool;
        }

        public void BeginExecution()
        {
            if (CurrentLayer == null)
                return;

            previousTexture = CurrentLayer.Texture.TextureItem;
            xnaComponent.BeginExecution();
        }

        public void Execute()
        {
            if (CurrentLayer == null)
                return;

            xnaComponent.Execute();
        }

        public void EndExecution()
        {
            if (CurrentLayer == null)
                return;

            xnaComponent.EndExecution(CreateUndoPoint);
        }

        public void UpdatePenData(Point position, float pressurePercentage)
        {
            xnaComponent.DrawingBoardData.PenPosition = new Microsoft.Xna.Framework.Vector2((float)position.X, (float)position.Y);
            xnaComponent.DrawingBoardData.PressurePercentage = pressurePercentage;
        }

        public void CreateUndoPoint()
        {
            if (CurrentLayer == null || xnaComponent.DrawTool == null)
                return;

            CurrentLayer.UndoRedoController.AddItem(new UndoRedoTextureItem(CurrentLayer, previousTexture));
            OnPropertyChanged("UndoRedoController");
        }

        public void SetScale(int scalePercentage)
        {
            xnaComponent.DrawingBoardData.Scale = (float)scalePercentage / 100;
        }

        public void SetAnimationSelectionInfo(AnimationSelectionInfo animationSelectionInfo)
        {
            xnaComponent.DrawingBoardData.AnimationSelectionInfo = animationSelectionInfo;
        }

        public void SetCurrentLayer(TextureData layer)
        {
            xnaComponent.DrawingBoardData.TextureData = layer;
            OnPropertyChanged("UndoRedoController");
        }

        public void SetCursorVisibility(bool value)
        {
            xnaComponent.DrawingBoardData.IsMouseInsideDrawingBoard = value;
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
